<?

	require_once("class.paymentprocessor.php");

	class PayPal extends PaymentProcessor

	{

		var $merchant_email;

		

		//

		// Constructor

		//

		function PayPal($merchant_email)

		{

			$this->merchant_email = $merchant_email;

		}

		

		

		function PostBack()

		{

			// read the post from PayPal system and add 'cmd'

			$req = 'cmd=_notify-validate';

			

			foreach ($this->input as $key => $value) 

			{

				$value = urlencode(stripslashes($value));

				$req .= "&$key=$value";

			}

			

			// post back to PayPal system to validate

			$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";

			$header .= "Content-Type: application/x-www-form-urlencoded\r\n";

			$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

			

			// open socket

			$fp = fsockopen ('paypal.com', 80, $errno, $errstr, 30);

						

			if (!$fp)

			{

				$this->Log("Cant connect to paypal");

				return false;

			}

			else 

			{

				//Push response

				fputs($fp, $header . $req);

				// Read response from server

				while (!feof($fp)) 

					$res = fgets($fp, 1024);

				

				// Close pointer

				fclose ($fp);

				

				return $res;

			}

		}

		

		function SetCurrency($currency)

		{

			$this->currency = $currency;

		}

		

		

		function ProccessSinglePayment()

		{
			$res = $this->PostBack();
			$retval = strcmp($res, "VERIFIED") == 0 && $this->input['mc_currency'] == $this->currency && $this->input['payment_status'] == "Completed" && $this->input['receiver_email'] == $this->merchant_email;
			
			// Log 
			if (!$retval)
			{
				$this->Log("Error while proccessing paypal payment");
				$this->Log($this->input);
				$this->Log("res = ".$res);
			}

	
			return $retval;
		}

	}

?>